﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
namespace NetCoreCMS.Web.Areas.Admin.Controllers.Users
{
    public class Group_EditController : ManageController
    {
        private string viewurl = "~/Areas/Admin/Views/Users/Group_Edit.cshtml";
        private string action = CMSEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;
        public IUser_groupsService _user_groups { get; set; }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            if (filterContext.Result != null)
                return;
            ChkAdminLevel("user_group", CMSEnums.ActionEnum.View.ToString()); //检查权限
            ActionResult result = View(EDIT_RESULT_VIEW);
            //取到操作类型
            string _action = Request.Query["action"];
            if (!string.IsNullOrEmpty(_action) && _action == CMSEnums.ActionEnum.Edit.ToString())
            {
                this.action = CMSEnums.ActionEnum.Edit.ToString();//修改类型
                int.TryParse(Request.Query["id"],out this.id);
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    filterContext.Result = result;
                    return;
                }
                if (!_user_groups.Exists(this.id))
                {
                    JscriptMsg("用户组不存在或已被删除！", "back");
                    filterContext.Result = result;
                    return;
                }
            }
            ViewBag.Action = this.action;
            ViewBag.Id = this.id.ToString();
        }
        //
        // GET: /admin/Group_Edit/

        public ActionResult Index()
        {
            if (action == CMSEnums.ActionEnum.Edit.ToString())
            {
                ShowInfo(this.id);
            }
            else
            {
                User_groups model = new User_groups();
                ViewData["model"] = model;
            }
            return View(viewurl);
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            var model = _user_groups.GetModel(_id);
            ViewData["model"] = model;
        }
        #endregion

        #region 增加操作=================================
        private int DoAdd(bool isLock, bool isDefault, bool isUpgrade)
        {
            int result = 0;
            User_groups model = new User_groups();

            model.title = Request.Form["txtTitle"].FirstOrDefault().Trim();
            model.is_lock = isLock ? 1 : 0;
            model.is_default = isDefault ? 1 : 0;
            model.is_upgrade = isUpgrade ? 1 : 0;
            model.grade = int.Parse(Request.Form["txtGrade"].FirstOrDefault().Trim());
            model.upgrade_exp = int.Parse(Request.Form["txtUpgradeExp"].FirstOrDefault().Trim());
            model.amount = decimal.Parse(Request.Form["txtAmount"].FirstOrDefault().Trim());
            model.point = int.Parse(Request.Form["txtPoint"].FirstOrDefault().Trim());
            model.discount = int.Parse(Request.Form["txtDiscount"].FirstOrDefault().Trim());
            result = _user_groups.Add(model);
            if (result > 0)
            {
                AddAdminLog(CMSEnums.ActionEnum.Add.ToString(), "添加用户组:" + model.title); //记录日志
            }
            return result;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id, bool isLock, bool isDefault, bool isUpgrade)
        {
            bool result = false;
            User_groups model = _user_groups.GetModel(_id);

            model.title = Request.Form["txtTitle"].FirstOrDefault().Trim();
            model.is_lock = isLock ? 1 : 0;
            model.is_default = isDefault ? 1 : 0;
            model.is_upgrade = isUpgrade ? 1 : 0;
            model.grade = int.Parse(Request.Form["txtGrade"].FirstOrDefault().Trim());
            model.upgrade_exp = int.Parse(Request.Form["txtUpgradeExp"].FirstOrDefault().Trim());
            model.amount = decimal.Parse(Request.Form["txtAmount"].FirstOrDefault().Trim());
            model.point = int.Parse(Request.Form["txtPoint"].FirstOrDefault().Trim());
            model.discount = int.Parse(Request.Form["txtDiscount"].FirstOrDefault().Trim());
            if (_user_groups.Update(model))
            {
                AddAdminLog(CMSEnums.ActionEnum.Edit.ToString(), "修改用户组:" + model.title); //记录日志
                result = true;
            }

            return result;
        }
        #endregion


        // 提交保存
        [HttpPost]
        public ActionResult SubmitSave(bool rblIsLock, bool rblIsDefault, bool rblIsUpgrade)
        {
            action = Request.Form["action"];
            this.id = int.Parse(Request.Form["id"]);
            if (action == CMSEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("user_group", CMSEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id, rblIsLock, rblIsDefault, rblIsUpgrade))
                {
                    JscriptMsg("保存过程中发生错误！", "");
                }
                else
                {
                    JscriptMsg("修改用户组成功！", "../group_list/index");
                }
            }
            else //添加
            {
                ChkAdminLevel("user_group", CMSEnums.ActionEnum.Add.ToString()); //检查权限
                this.id = DoAdd(rblIsLock, rblIsDefault, rblIsUpgrade);
                if (id <= 0)
                {
                    JscriptMsg("保存过程中发生错误！", "");
                }
                else
                {
                    JscriptMsg("添加会员组成功！", "../group_list/index");
                }
            }
            return View(EDIT_RESULT_VIEW);
        }
    }
}
